package com.epfresh.supplier.dialog;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.epfresh.api.utils.FormatUtil;
import com.epfresh.api.widget.Dialog.BaseDialogView;
import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.GrouponInfo;

/**
 * @author wangzhanghuan
 * @version 1.0
 * @date create 17-8-23
 * @Description
 */
public class GrouponEnsureOrderDialog extends BaseDialogView {

    TextView mTvAddressStart;
    TextView mTvAddressEnd;

    TextView mTvDeliveryDate;
    TextView mTvAmount;
    TextView mTvCost;
    TextView mTvFreight;
    TextView mTvTotal;
    TextView mTvHandSel;
    TextView mTvHandSelLast;

    View.OnClickListener onPayListener;

    public View.OnClickListener getOnPayListener() {
        return onPayListener;
    }

    public void setOnPayListener(View.OnClickListener onPayListener) {
        this.onPayListener = onPayListener;
    }

    public GrouponEnsureOrderDialog(Context context) {
        super(context);
        setInAnim(true);
        setOutAnim(true);
        inAnim.setDuration(300);
        outAnim.setDuration(300);

    }

    public void show(GrouponInfo mGrouponInfo) {
        if (mGrouponInfo == null) {
            return;
        }
        super.show();
        mTvAddressStart.setText(mGrouponInfo.getBaseCityName());
        mTvAddressEnd.setText(mGrouponInfo.getAgencyCityName());
        mTvDeliveryDate.setText(mGrouponInfo.getDeliveryInfoFull());
        String count = mGrouponInfo.getProductCount();
        String freightUnit = mGrouponInfo.getFreightUnit();
        String unitPrice = FormatUtil.subZeroAndDot(mGrouponInfo.getUnitPrice());
        String deposit = mGrouponInfo.getDeposit();
        String depositLast = mGrouponInfo.getDepositLast();

        String amount = count + mGrouponInfo.getChargeUnit() + "*" + FormatUtil.subZeroAndDot(unitPrice) + "元";
        mTvAmount.setText(amount);
        double cost = FormatUtil.countMultiply(count,unitPrice);
        double freight = FormatUtil.countMultiply(freightUnit,count);
        double total = FormatUtil.countAdd(cost,freight);

        mTvCost.setText(FormatUtil.subZeroAndDot(cost)+"元");
        mTvFreight.setText(FormatUtil.subZeroAndDot(freight)+"元");


        mTvTotal.setText(FormatUtil.subZeroAndDot(total)+"元");

        double v = FormatUtil.countMultiply(cost + "", deposit);
        v = FormatUtil.countDivide(v,100);
        mTvHandSel.setText(FormatUtil.subZeroAndDot(v+"")+"元");
        v = FormatUtil.countAdd(total,-v);

        mTvHandSelLast.setText(FormatUtil.subZeroAndDot(v+"")+"元");

    }

   public Animation getInAnimation() {

        return AnimationUtils.loadAnimation(this.context, com.epfresh.api.R.anim.anim_bottom_in);
    }

    public Animation getOutAnimation() {
        return AnimationUtils.loadAnimation(this.context, com.epfresh.api.R.anim.anim_bottom_out);
    }



   @Override
    protected void initRes() {
        outContainerRes = R.id.f_out_container;
        containerRes = R.id.f_content_container;
        rootRes = R.layout.dialog_content_view;
       rootBgRes = R.id.f_v_content_bg;
    }

   protected ViewGroup getDecorView(Context context) {
        return (ViewGroup) ((Activity) context).getWindow().getDecorView().findViewById(android.R.id.content);
    }

    @Override
    protected void createdView() {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View rootView = layoutInflater.inflate(R.layout.dialog_groupon_ensure_order, null, false);
        ViewGroup.LayoutParams lp = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        rootView.setLayoutParams(lp);
        setContentView(rootView);
//        rootView.setClickable(true);
        setCancelable(true);
        initView();
    }



    private void initView() {

        findView(R.id.iv_closed).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        findView(R.id.tv_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (onPayListener != null) {
                    onPayListener.onClick(view);
                }
            }
        });

        mTvAddressStart = findView(R.id.tv_address_1);
        mTvAddressEnd = findView(R.id.tv_address_2);
        mTvDeliveryDate = findView(R.id.tv_delivery_date);
        mTvAmount = findView(R.id.tv_ammount);
        mTvCost = findView(R.id.tv_cost);
        mTvFreight = findView(R.id.tv_freight);
        mTvTotal = findView(R.id.tv_total);
        mTvHandSel = findView(R.id.tv_handsel);
        mTvHandSelLast = findView(R.id.tv_handsel_last);





    }





}
